package com.lagou.bigdata.task01;

import java.io.*;;
import java.util.List;

public class DaoStudent {
    private List<Student> students;

    public DaoStudent(List<Student> students) {
        this.students = students;
    }

    // 定义方法将集合写入到文件中
    public void writeStudentList() {

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("./students.txt"));
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if ( null != oos) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 定义方法读取文件中的所有学生信息
    public List<Student> readStudentList() throws IOException, ClassNotFoundException {
        if (new File("./student").exists()) {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("./student.txt"));

            students = (List<Student>) ois.readObject();
            for(Student s : students){
                System.out.println(s);
            }
            ois.close();
        }
        return students;
    }


}
